layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl'], function(){
    var $ = layui.$,
        table = layui.table;

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'banner/list',
        toolbar: '#data-table-toolbar',
        title: 'banner列表',
        height: globalHeight,
        page: true,
        cols: [[
            {type: 'checkbox'},
            {field: 'id', title: 'ID', width: 80, align: 'center'},
            {field: 'img', title: 'banner图片', width: 200, align: 'center', templet: '#imgTpl'},
            {field: 'name', title: 'banner名称', align: 'center'},
            {field: 'url', title: '跳转路径', align: 'center'},
            {field: 'type', title: 'banner类型', width: 120, align: 'center', templet: '#typeTpl'},
            {field: 'orderId', title: '排序', width: 120, align: 'center'},
            {field: 'status', title: '状态', width: 120, align: 'center', templet: '#statusTpl'},
            {field: 'createTime', title: '创建时间', width: 220, align: 'center'},
            {field: 'op', title: '操作', width: 220, align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.banners //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'add':
                layer.open({
                    type: 2,
                    title: '添加banner',
                    offset: '15%',
                    content: 'toEditBanner',
                    area: ['600px', '480px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {     //确定按钮回调
                        var iframeWindow = window['layui-layer-iframe'+ index],
                            submit = layero.find('iframe').contents().find("#data-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(data-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            // 加载层
                            var load = layerLoad();

                            //提交 Ajax 成功后，静态更新表格中的数据
                            post('banner/add', field, field, function (data) {
                                layer.msg("添加成功");
                                table.reload('data-table');
                                layer.close(index); //关闭弹层
                                layer.close(load); //关闭弹层
                            });
                        });

                        submit.trigger('click');
                    }
                });
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
                type: 2,
                title: '编辑banner',
                offset: '15%',
                content: 'toEditBanner',
                area: ['600px', '480px'],
                btn: ['确定', '取消'],
                success: function(layero, index){   //弹窗成功后回调
                    var iframeWindow = window['layui-layer-iframe' + index];
                    $(iframeWindow.document).find('#id').val(data.id);   //传值到iframe页面
                },
                yes: function (index, layero) {     //确定按钮回调
                    var iframeWindow = window['layui-layer-iframe'+ index],
                        submit = layero.find('iframe').contents().find("#data-submit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(data-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        // 加载层
                        var load = layerLoad();
                        post('banner/add', field, false, function (resultData) {
                            layer.close(index); //关闭弹层
                            layer.close(load); //关闭弹层
                            table.reload('data-table');
                            layer.msg("更新成功");
                        });
                    });

                    submit.trigger('click');
                },
            });
        } else if(obj.event === 'updateStatus'){
            var str = "禁用";
            if(data.status == 1){
                str = "启用";
            }
            layer.confirm('确定' + str + '该banner', {offset: '30%'}, function(index){
                // 加载层
                var load = layerLoad();
                post('banner/updateStatus', {'id': data.id}, false, function (data) {
                    table.reload('data-table');
                    layer.msg(str + "成功");
                    layer.close(index);
                    layer.close(load);
                });
            });
        }
    });
});